﻿using MediatR;
using Newtonsoft.Json;
using Product.Domain;
using Product.Domain.DTO;
using Product.Infrstructre.Impl;
using Product.Read.API.Applcation.Command.Product;
using Product.Read.API.Dtos;
using WMSpublic;
using static System.Net.Mime.MediaTypeNames;
using static WMSpublic.ParseAPI;

namespace Product.Read.API.Applcation.CommandHandlers
{
    /// <summary>
    /// 产品分页显示
    /// </summary>
    public class ProductInfoSelectCommandHandler : IRequestHandler<ProductInfoSelectCommand, ProductDTO>
    {
        //产品
        protected readonly IbaseProductInfoRepository ibaseProductInfoRepository;
        //产品类别
        protected readonly IbaseProductRepository ProType;

        public ProductInfoSelectCommandHandler(IbaseProductInfoRepository ibaseProductInfoRepository, IbaseProductRepository proType)
        {
            this.ibaseProductInfoRepository = ibaseProductInfoRepository;
            ProType = proType;
        }

        public async Task<ProductDTO> Handle(ProductInfoSelectCommand request, CancellationToken cancellationToken)
        {
            List<Vendor> vend = await GetVendorInfo(1, 1000);
            var newlist = from a in await ibaseProductInfoRepository.GetAllAsync()
                          join b in await ProType.GetAllAsync() on a.ProductTypeID equals b.ProductTypeID
                          join c in vend on a.VendorId equals c.VendorId
                          where a.IsDel == false && a.Office_Id == request.Office_Id
                          select new newProductDto
                          {
                              WarehouseId = a.WarehouseId,
                              ProductsID = a.ProductsID,
                              ProductsCode = a.ProductsCode,
                              ProductsName = a.ProductsName,
                              ProductTypeID = a.ProductTypeID,
                              VendorsCode = a.VendorsCode,
                              InteriorCode = a.InteriorCode,
                              Specs = a.Specs,
                              StorageUnits = a.StorageUnits,
                              Upperlimit = a.Upperlimit,
                              Lowerlimit = a.Lowerlimit,
                              WrapType = a.WrapType,
                              Price = a.Price,
                              VendorId = a.VendorId,
                              ProductTypeName = b.ProductTypeName,
                              VendorName = c.VendorName,
                              ProcurementNum = a.ProcurementNum,
                              StorehouseId = a.StorehouseId,
                              AddTime = a.AddTime
                          };

            var size = (request.pageindex - 1) * request.pagesize;
            if (request.ProductsCode != null)
            {
                newlist = newlist.Where(x => x.ProductsCode.Contains(request.ProductsCode)).ToList();
            }
            if (request.ProductsName != null)
            {
                newlist = newlist.Where(x => x.ProductsName.Contains(request.ProductsName)).ToList();
            }
            var page = new ProductDTO();
            page.pagecount = newlist.Count();
            page.totalcount = (int)Math.Ceiling(page.pagecount * 1.0 / request.pagesize);
            page.list = newlist.ToList();
            return page;
        }


        /// <summary>
        /// 获取供应商信息
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<dynamic> GetVendorInfo(int pageindex, int pagesize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://10.223.8.21:8000/ywd/api/Vendor/GetShowVendor?pageindex=" + pageindex + "&pagesize=" + pagesize;
            string postRep = await client.GetAsync(url);
            var vendors = JsonConvert.DeserializeObject<ApiResultList<Vendor>>(postRep);
            return vendors.data.list;
        }


        /// <summary>
        /// 供应商
        /// </summary>
        public class Vendor
        {
            public int VendorId { get; set; }
            /// <summary>
            /// 供应商编号
            /// </summary>
            public string? VendorCode { get; set; }
            /// <summary>
            /// 供应商名称
            /// </summary>
            public string? VendorName { get; set; }
            /// <summary>
            /// 供应商类型
            /// </summary>
            public string? VendorType { get; set; }
            /// <summary>
            /// 电话
            /// </summary>
            public string? Vphone { get; set; }
            /// <summary>
            /// 传真
            /// </summary>
            public string? Vfax { get; set; }
            /// <summary>
            /// Email
            /// </summary>
            public string? Email { get; set; }
            /// <summary>
            /// 联系人
            /// </summary>
            public string? Vcontact { get; set; }
            /// <summary>
            /// 地址
            /// </summary>
            public string? Vaddress { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime? CreatTime { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 状态
            /// </summary>
            public int? State { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }

        }
    }
}
